package com.example.bomc.service.fixStaffService.impl;

import com.example.bomc.common.msg.CodeMsg;
import com.example.bomc.dao.ManageDAO;
import com.example.bomc.dao.UserDAO;
import com.example.bomc.model.FixStaff;
import com.example.bomc.model.User;
import com.example.bomc.service.fixStaffService.FixStaffService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FixStaffServiceImpl implements FixStaffService {

    @Autowired
    ManageDAO manageDAO;
    @Autowired
    UserDAO userDAO;
    @Override
    public CodeMsg examined(String name) {
        CodeMsg msg = new CodeMsg();
        if (name.isEmpty()){
            msg.setRetCode(-1);
            msg.setMessage("用户名为空！！！");
            return msg;
        }

        //判断用户是否存在
        User user = userDAO.selectByName(name);
        if (user == null){
            msg.setRetCode(-1);
            msg.setMessage("用户不存在！！！");
            return msg;
        }
        manageDAO.updateExamined(name);
        msg.setRetCode(0);
        msg.setMessage("审核成功！！！");
        return msg;
    }

    @Override
    public CodeMsg addStaff(FixStaff staff) {
        CodeMsg msg = new CodeMsg();
        if(staff == null){
            msg.setRetCode(-1);
            msg.setMessage("添加异常！！！");
            return msg;
        }
        manageDAO.addFixStaff(staff);
        msg.setRetCode(0);
        msg.setMessage("添加成功！！！");
        return msg;
    }

    @Override
    public CodeMsg delStaff(String fixname) {
        CodeMsg msg = new CodeMsg();
        if (fixname.isEmpty()){
            msg.setRetCode(-1);
            msg.setMessage("用户名为空！！！");
            return msg;
        }

        //判断维护人员是否存在
        FixStaff staff = manageDAO.selectByFixName(fixname);
        if (staff == null){
            msg.setRetCode(-1);
            msg.setMessage("维修人员不存在！！！");
            return msg;
        }
        manageDAO.deleteStaff(fixname);
        msg.setRetCode(0);
        msg.setMessage("删除成功！！！");
        return msg;
    }

    @Override
    public CodeMsg fenpeiFix(String name, String fixname, String trouble) {
        CodeMsg msg = new CodeMsg();
        if (name.isEmpty()){
            msg.setRetCode(-1);
            msg.setMessage("用户名为空！！！");
            return msg;
        }

        //判断用户是否存在
        User user = userDAO.selectByName(name);
        if (user == null){
            msg.setRetCode(-1);
            msg.setMessage("用户不存在！！！");
            return msg;
        }
        //判断维护人员是否存在
        FixStaff staff = manageDAO.selectByFixName(fixname);
        if (staff == null){
            msg.setRetCode(-1);
            msg.setMessage("维修人员不存在！！！");
            return msg;
        }

        manageDAO.fenpeiFix(name, fixname, trouble);
        msg.setRetCode(0);
        msg.setMessage("分配成功！！！");
        return msg;
    }

    @Override
    public CodeMsg selAssess() {
        CodeMsg msg = new CodeMsg();
        manageDAO.selSameQuery();
        msg.setRetCode(0);
        msg.setMessage("查询成功！！！");
        return null;
    }
}
